﻿using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;

namespace Marmat.Forms
{
    public partial class TreeNode
    {

        /* TODO ERROR: Skipped RegionDirectiveTrivia */
        internal Rectangle rect_Text { get; set; }
        internal Rectangle Rect_Checked { get; set; }
        internal Rectangle Rect_Image { get; set; }
        [DefaultValue(null)]
        public Image Image { get; set; }

        internal Rectangle Rectangle
        {
            get
            {
                return this.m_Rectangle;
            }
        }

        internal Rectangle m_Rectangle;

        internal Rectangle Rect_ShowPlusMinus { get; set; }
        /* TODO ERROR: Skipped EndRegionDirectiveTrivia */
        /// <summary>
    /// 获取上一个同级树节点。
    /// </summary>
    /// <value></value>
    /// <returns></returns>
    /// <remarks></remarks>
        [Browsable(false)]
        public TreeNode PrevNode
        {
            get
            {
                TreeNode nd = null;
                if (this.Parent is object)
                {
                    foreach (TreeNode Item in this.Parent.Nodes)
                    {
                        if (ReferenceEquals(Item, this))
                        {
                            return nd;
                        }

                        nd = Item;
                        break;
                    }
                }
                else if (this.TreeView is object)
                {
                    foreach (TreeNode Item in this.TreeView.Nodes)
                    {
                        if (ReferenceEquals(Item, this))
                        {
                            return nd;
                        }

                        nd = Item;
                        break;
                    }
                }

                return nd;
            }
        }

        /// <summary>
    /// 获取下一个同级树节点。
    /// </summary>
    /// <value></value>
    /// <returns></returns>
    /// <remarks></remarks>
        [Browsable(false)]
        public TreeNode NextNode
        {
            get
            {
                TreeNode nd = null;
                if (this.Parent is object)
                {
                    bool boo = false;
                    foreach (TreeNode Item in this.Parent.Nodes)
                    {
                        if (boo == true)
                        {
                            return Item;
                        }

                        if (ReferenceEquals(Item, this))
                        {
                            boo = true;
                        }

                        nd = Item;
                    }
                }
                else if (this.TreeView is object)
                {
                    bool boo = false;
                    foreach (TreeNode Item in this.TreeView.Nodes)
                    {
                        if (boo == true)
                        {
                            return Item;
                        }

                        nd = Item;
                        if (ReferenceEquals(Item, this))
                        {
                            boo = true;
                        }
                    }
                }

                return nd;
            }
        }

        [DefaultValue(null)]
        public object Tag { get; set; }

        [Browsable(false)]
        public TreeView TreeView
        {
            get
            {
                return this.m_TreeView;
            }
        }

        internal TreeView m_TreeView;

        [Browsable(false)]
        [Editor(typeof(TreeViewEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Localizable(true)]
        [NotifyParentProperty(true)]
        public TreeNodeCollection Nodes
        {
            get
            {
                if (this.m_Nodes is null)
                {
                    this.m_Nodes = new TreeNodeCollection(this.TreeView, this);
                }

                return this.m_Nodes;
            }
        }

        private TreeNodeCollection m_Nodes;

        /// <summary>
    /// 获取或设置一个值，用以指示是否在树视图控件中的树节点旁显示复选框。
    /// </summary>
    /// <value></value>
    /// <returns>如果在树视图控件中的每个树节点旁显示复选框，则为 true；否则为 false。默认值为 false。</returns>
    /// <remarks></remarks>
        [DefaultValue(false)]
        public bool CheckBoxes { get; set; } = false;

        [Browsable(false)]
        public bool IsExpanded
        {
            get
            {
                return this.m_IsExpanded;
            }
        }

        internal bool m_IsExpanded
        {
            get
            {
                return this.m_m_IsExpanded;
            }

            set
            {
                if (this.m_m_IsExpanded != value)
                {
                    this.m_m_IsExpanded = value;
                    if (this.Nodes.Count > 0)
                    {
                        var nd = this.Nodes[this.Nodes.Count - 1];
                        var rect = new Rectangle(this.Rectangle.X, this.Bounds.Y, this.TreeView.Width, this.TreeView.Height - this.Bounds.Y);
                        this.TreeView.Invalidate(rect);
                    }
                }
            }
        }

        private bool m_m_IsExpanded = true;

        public Color BackColor
        {
            get
            {
                if (this.m_BackColor == Color.Empty && this.TreeView is object)
                {
                    return this.TreeView.BackColor;
                }

                return this.m_BackColor;
            }

            set
            {
                this.m_BackColor = value;
            }
        }

        private Color m_BackColor = Color.Empty;

        [Browsable(false)]
        public Rectangle Bounds
        {
            get
            {
                return this.m_Bounds;
            }
        }

        internal Rectangle m_Bounds;

        public string Text
        {
            get
            {
                if (string.IsNullOrEmpty(this.m_Text))
                {
                    return this.Name;
                }

                return this.m_Text;
            }

            set
            {
                this.m_Text = value;
                if (this.TreeView is object && this.rect_Text.Width > 0 && this.rect_Text.Height > 0)
                {
                    this.TreeView.Invalidate(this.Rectangle);
                }
            }
        }

        private string m_Text;

        public string Name
        {
            get
            {
                return this.m_name;
            }

            set
            {
                this.m_name = value;
                if (this.TreeView is object && this.rect_Text.Width > 0 && this.rect_Text.Height > 0)
                {
                    this.TreeView.Invalidate(this.Rectangle);
                }
            }
        }

        private string m_name;

        [Browsable(false)]
        public TreeNode Parent
        {
            get
            {
                return this.m_Parent;
            }
        }

        internal TreeNode m_Parent;

        [Browsable(false)]
        public int Level
        {
            get
            {
                int m_Level = 0;
                var Parent = this;
                do
                {
                    if (Parent.Parent is object)
                    {
                        Parent = Parent.Parent;
                        m_Level += 1;
                    }
                    else
                    {
                        break;
                    }
                }
                while (true);
                return m_Level;
            }
        }

        [Browsable(false)]
        [DefaultValue(false)]
        public bool Checked
        {
            get
            {
                return this.m_Checked;
            }

            set
            {
                if (this.m_Checked != value)
                {
                    this.m_Checked = value;
                    this.TreeView.Invalidate(this.Bounds);
                }
            }
        }

        private bool m_Checked = false;

        public void Collapse()
        {
            this.Collapse(true);
        }

        public void Collapse(bool ignoreChildren)
        {
            this.m_IsExpanded = false;
            if (ignoreChildren)
            {
                var Items = this.TreeView.GetAllTreeNode(this);
                foreach (TreeNode Item in Items)
                    Item.m_IsExpanded = false;
            }

            this.TreeView.Invalidate();
        }

        [DefaultValue(null)]
        public ContextMenuStrip ContextMenuStrip { get; set; }

        public void EnsureVisible()
        {
        }

        public void Expand()
        {
            this.m_IsExpanded = true;
        }

        public void ExpandAll()
        {
            var Items = this.TreeView.GetAllTreeNode(this);
            Items.Insert(0, this);
            foreach (TreeNode Item in Items)
                Item.m_IsExpanded = true;
            this.TreeView.Invalidate();
        }
    }
}